package org.aeon.colorfulplanebattle.flying.thread;

import org.aeon.colorfulplanebattle.constant.DirectionE;
import org.aeon.colorfulplanebattle.flying.entity.Ship;

import java.util.List;
import java.util.Random;

/**
 * @author: MissingAeon
 * @date: 2024/1/9 15:48
 * @description: 飞船线程
 */
public class ShipThread extends Thread {
    private final List<Ship> ships;
    private boolean flag = true;

    public ShipThread(List<Ship> ships) {
        this.setName("飞船线程");
        this.ships = ships;
    }


    @Override
    public void run() {
        int i, len;
        Random random = new Random();
        DirectionE[] values = DirectionE.values();
        int lenD = values.length;
        while (flag) {
            len = ships.size();
            for (i = 0; i < len; i++) {
                try {

                    Ship ship = ships.get(i);
                    if (!ship.move(DirectionE.DOWN)) {
                        ship.setDestroy(true);
                    }
                    // 这里设置子弹概率
                    // if (random.nextInt(50) == 0)
                    //     ship.attack();
                } catch (ArrayIndexOutOfBoundsException ignored) {
                    break;
                }
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException ignored) {
            }
        }
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
